package com.net.wanjian.networkhospitalmanager.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.activity.skilltrain.StudentHistoryLabDetailsActivity;
import com.net.wanjian.networkhospitalmanager.activity.skilltrain.StudentHistoryLaboratoryListActivity;
import com.net.wanjian.networkhospitalmanager.adapter.StudentHistoryReserveAdapter;
import com.net.wanjian.networkhospitalmanager.adapter.base.RecyclerBaseAdapter;
import com.net.wanjian.networkhospitalmanager.bean.StudentHistoryLabReserveResult;
import com.net.wanjian.networkhospitalmanager.consts.AppConsts;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.net.wanjian.networkhospitalmanager.view.NoDataEmptyView;
import com.net.wanjian.networkhospitalmanager.widge.RefreshRecyclerView;

import java.util.List;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

public class HistoryReserveListActivity extends BaseActivity{

    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.top_title_tv)
    TextView topTitleTv;
    @BindView(R.id.laboratory_list_recycler_view)
    RefreshRecyclerView myReserveRecyclerView;
    @BindView(R.id.laboratory_list_nodata_view)
    NoDataEmptyView noDataLayout;
    @BindView(R.id.laboratory_progress)
    ProgressBar mProgressBar;

    private int currentNum = AppConsts.currentPageNum, currentPageNum,countNum;
    private StudentHistoryReserveAdapter studentHistoryReserveAdapter;
    private List<StudentHistoryLabReserveResult.DataBean.PastLabReserveListBean> mCurrentLabReserveList;

    @Override
    protected void initData() {
        topTitleTv.setText("历史预约");
        topBackLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HistoryReserveListActivity.this.finish();
            }
        });

        noDataLayout.setNoData(R.string.no_lab_reserve_text);
        noDataLayout.setOnReLoadClickListener(new NoDataEmptyView.OnReLoadClickListener() {
            @Override
            public void onReloadClick(View v) {
                getStudentHistoryReserveHttpRequest();
            }
        });
        myReserveRecyclerView.setEmptyView(noDataLayout);

        myReserveRecyclerView.setRefreshMode(3);
        myReserveRecyclerView.addOnRefreshListener(new RefreshRecyclerView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getStudentHistoryReserveHttpRequest();
            }

            @Override
            public void onLoadMore() {
                loadMore();
            }
        });

        myReserveRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        myReserveRecyclerView.refresh();

    }


    private void loadMore() {
        currentPageNum++;
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.studentHistoryLabReserveList(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(), xmlUtil.getUserIdentityId(),
                currentPageNum, currentNum, new DisposableObserver<StudentHistoryLabReserveResult>() {
                    @Override
                    public void onNext(StudentHistoryLabReserveResult studentHistoryLabReserveResult) {

                        if (studentHistoryLabReserveResult.getResult().equals(ErrorCodeReturn.success + "")) {
                            SharedXmlUtil.getInstance().setToken(URLDecoderUtil.getDecoder(studentHistoryLabReserveResult.getData().getToken()));
                            List<StudentHistoryLabReserveResult.DataBean.PastLabReserveListBean> list = studentHistoryLabReserveResult.getData().getPastLabReserveList();
                            mCurrentLabReserveList.addAll(list);
                            studentHistoryReserveAdapter.setList(mCurrentLabReserveList);

                            if (list.size() < currentNum || mCurrentLabReserveList.size() >= countNum){
                                myReserveRecyclerView.setNoMore(true);
                            }else {
                                myReserveRecyclerView.loadMoreComplete();
                            }

                        } else {
                            //查询失败
                            myReserveRecyclerView.loadMoreComplete();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        myReserveRecyclerView.loadMoreComplete();
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * 获取学生实验室预约列表
     */
    public void getStudentHistoryReserveHttpRequest() {
        currentPageNum = 0;
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.studentHistoryLabReserveList(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(), xmlUtil.getUserIdentityId(),
                currentPageNum, currentNum, new DisposableObserver<StudentHistoryLabReserveResult>() {
                    @Override
                    public void onNext(StudentHistoryLabReserveResult studentHistoryLabReserveResult) {

                        if (studentHistoryLabReserveResult.getResult().equals(ErrorCodeReturn.success + "")) {
                            SharedXmlUtil.getInstance().setToken(URLDecoderUtil.getDecoder(studentHistoryLabReserveResult.getData().getToken()));
                            mCurrentLabReserveList = studentHistoryLabReserveResult.getData().getPastLabReserveList();
                            countNum = Integer.parseInt(studentHistoryLabReserveResult.getData().getTotalCount());
                            Log.e(TAG, "onNext: "+countNum);
                            initRecyclerList();

                            if (studentHistoryLabReserveResult.getData().getPastLabReserveList().size() < currentNum || mCurrentLabReserveList.size() >= countNum) {
                                myReserveRecyclerView.refreshComplete();
                                myReserveRecyclerView.setNoMore(true);
                            } else {
                                myReserveRecyclerView.refreshComplete();
                                myReserveRecyclerView.setNoMore(false);
                            }
                        } else {
                            myReserveRecyclerView.refreshComplete();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        myReserveRecyclerView.refreshComplete();
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    private void initRecyclerList() {
        mProgressBar.setVisibility(View.GONE);
        studentHistoryReserveAdapter = new StudentHistoryReserveAdapter(this);
        studentHistoryReserveAdapter.setList(mCurrentLabReserveList);
        myReserveRecyclerView.setAdapter(studentHistoryReserveAdapter);


        studentHistoryReserveAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int pos) {
                Bundle bundle = new Bundle();
                bundle.putString(StudentHistoryLabDetailsActivity.LAB_RESERVE_ID_KEY, URLDecoderUtil.getDecoder(mCurrentLabReserveList.get(pos).getLabReserveID()));
                bundle.putString(StudentHistoryLaboratoryListActivity.LAB_RESERVE_MEMBER_ID_KEY,URLDecoderUtil.getDecoder(mCurrentLabReserveList.get(pos).getLabReserveMemberID()));
                //这里传递当前item数据，为了详情页面判断按钮的状态
                bundle.putSerializable(StudentHistoryLabDetailsActivity.LAB_RESERVE_DATA_KEY, mCurrentLabReserveList.get(pos));
                bundle.putInt(StudentHistoryLabDetailsActivity.LAB_RESERVE_TYPE_KEY, 33);
                openActivity(StudentHistoryLabDetailsActivity.class, bundle);
            }
        });
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        noDataLayout.setNoData(R.string.no_lab_reserve_text);
        myReserveRecyclerView.setEmptyView(noDataLayout);
        getStudentHistoryReserveHttpRequest();
    }

    @Override
    protected void onNetworkDisConnected() {
        mCurrentLabReserveList = null;

        noDataLayout.setNoNetWork();
        myReserveRecyclerView.setEmptyView(noDataLayout);
        studentHistoryReserveAdapter.notifyDataSetChanged();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_history_reserve_list;
    }

}
